#include "interaction.h"
#include <QDateTime>
#include <QRandomGenerator>
#include <QDebug>

Interaction::Interaction(QObject *parent) : QObject(parent)
{

}

int Interaction::getRandomX()
{
    int randomX;
    do{
        randomX = QRandomGenerator::global()->bounded(m_currentScreenWidth - 100);
    }while(!isValidRandom(randomX));
    aAuxiliaryArr.push_back(randomX);
    return randomX;
}

int Interaction::getRandomY()
{
    int randomY = 0 - QRandomGenerator::global()->bounded(500);
    // 如果要为任务球设置y，就一定需要考虑，当任务球未进入屏幕时，不能被键盘给抹杀掉
    return randomY;
}

QString Interaction::getRandomValue()
{
    //  33 ~ 126 都是可以在键盘上敲击的字符的ASCII值
    int randomNum = QRandomGenerator::global()->bounded(94) + 33;
    QString randomValue = static_cast<QString>(randomNum);
    qDebug() << "randomValue = " << randomValue;
    if(aAuxiliaryArr.size() == m_onceGerneratedQuantity) {
        aAuxiliaryArr.clear();
    }
    return randomValue;
}

QString Interaction::getRandomImgUrl()
{
    // 这是一张 url 表
    QStringList aList = {"qrc:/Images/green.png",
                         "qrc:/Images/orange.png",
                         "qrc:/Images/yellow.png"};
    int aRandomNum = QRandomGenerator::global()->bounded(3);

    return aList.at(aRandomNum);
}

QString Interaction::getRandomEndImgUrl()
{
    // 这是一张 url 表
    QStringList aList = {"qrc:/Images/blueA.png",
                         "qrc:/Images/greenA.png",
                         "qrc:/Images/greenB.png",
                        "qrc:/Images/orangeA.png",
                        "qrc:/Images/orangeB.png"};
    int aRandomNum = QRandomGenerator::global()->bounded(5);
    return aList.at(aRandomNum);
}

// 用于判断生成的随机数是否满足业务逻辑，防止任务球球发生重叠
bool Interaction::isValidRandom(const int &aRandomNum)
{
    for(auto num : aAuxiliaryArr){
        int differValue = qAbs(num - aRandomNum);   // 差值
        if(differValue < 80) return false;
    }

    return true;
}


int Interaction::getOnceGerneratedQuantity() const
{
    return m_onceGerneratedQuantity;
}

void Interaction::setOnceGerneratedQuantity(int value)
{
    if(m_onceGerneratedQuantity == value)
        return;
    m_onceGerneratedQuantity = value;
    emit onceGerneratedQuantityChanged();
}


int Interaction::getCurrentScreenWidth() const
{
    return m_currentScreenWidth;
}

void Interaction::setCurrentScreenWidth(int value)
{
    if(m_currentScreenWidth == value)
        return;
    m_currentScreenWidth = value;
    emit currentScreenWidthChanged();
}

int Interaction::getCurrentScreenHeight() const
{
    return m_currentScreenHeight;
}

void Interaction::setCurrentScreenHeight(int value)
{
    if(m_currentScreenHeight == value)
        return;
    m_currentScreenHeight = value;
    emit currentScreenHeightChanged();
}

int Interaction::getPracticeType() const
{
    return m_practiceType;
}

void Interaction::setPracticeType(int value)
{
    if(m_practiceType == value)
        return;
    m_practiceType = value;
    emit practiceTypeChanged();
}


